<?php
/**
 * Created by XM·Tan.
 * Motto: Who shuts love out, in turn shall be shut out from love.
 * Date: 2021/3/23 1:27
 */

namespace app\admin\controller;


use app\admin\business\AdminAuth as AdminAuthBus;
use app\admin\validate\AdminAuth as AdminAuthValidate;
use app\admin\business\AdminRole;
use app\common\lib\ArrayLib;
use app\common\lib\Show;
use app\common\lib\Status;
use think\Exception;
use think\facade\View;

class AdminAuth extends AdminBase {

    public $business = null;
    public function __construct() {
        $this->business = new AdminAuthBus();
    }

    public function index() {
        return View::fetch();
    }

    // 列表数据获取 - 无需分页
    public function getLists() {
        $result = (new AdminAuthBus())->getAuthLists();
        return Show::show_lay_table(config('layui.success'), '', 0, $result);
    }

    // 添加/编辑 下拉框权限数据
    public function getTreeAuth() {
        $result = (new AdminAuthBus())->getAuthLists(['type' => 1]);
        $list = ArrayLib::setAuthArray($result);
        $list = ArrayLib::getTree($list);
        return json($list);
    }

    // 权限树数据获取
    public function getAuthTreeList() {
        $id = input('param.id', 0, 'intval');
        if (empty($id)) {
            return Show::error('参数不合法');
        }
        $result = (new AdminAuthBus())->getAuthLists();
        $list = ArrayLib::setAuthArray($result);

        $role = (new AdminRole())->getById($id);
        $list = ArrayLib::setAuthChecked($list, $role['rules']);

        $list = ArrayLib::getTree($list, 'list');
        return Show::show_lay_table(0, '', '', $list);
    }

    // 添加权限
    public function add() {
        if(request()->isPost()){

            $data = input('param.');
            // 使用 validate 验证机制验证参数
            $validate = new AdminAuthValidate();
            if (!$validate->scene('insert')->check($data)) {
                return Show::error($validate->getError());
            }

            // 执行 business 操作
            $res = (new AdminAuthBus())->insertData($data);
            if (!$res) {
                return Show::error('添加权限失败');
            }
            return Show::success('添加权限成功');

        } else {
            return View::fetch();
        }
    }

    // 编辑权限
    public function edit() {
        if(request()->isPost()){
            $data = input('param.');

            // 使用 validate 验证机制验证参数
            $validate = new AdminAuthValidate();
            if (!$validate->scene('update')->check($data)) {
                return Show::error($validate->getError());
            }

            // 执行 business 操作
            $res = (new AdminAuthBus())->updateData($data);
            if (!$res) {
                return Show::error('更新权限失败');
            }
            return Show::success('更新权限成功');

        } else {

            $id = input('param.id', 0, 'intval');
            if ($id == 0) {
                throw new Exception('请求不合法');
            }
            $info = (new AdminAuthBus())->getById($id);
            return View::fetch('', [
                'info' => $info,
            ]);
        }
    }

    /**
     * 是否主菜单
     * @return \think\response\Json
     * @throws Exception
     */
    public function type() {
        $id = input('param.id', 0, 'intval');
        $status = input('param.status', 0, 'intval');
        if (!$id || !in_array($status, Status::getTableStatus())) {
            return Show::error('参数错误');
        }

        $res = $this->business->setFiledToValue($id, $status, 'type');
        if ($res) {
            return Show::success();
        } else {
            return Show::error();
        }
    }

    /**
     * 是否主菜单
     * @return \think\response\Json
     * @throws Exception
     */
    public function is_hidden() {
        $id = input('param.id', 0, 'intval');
        $status = input('param.status', 0, 'intval');
        if (!$id || !in_array($status, Status::getTableStatus())) {
            return Show::error('参数错误');
        }

        $res = $this->business->setFiledToValue($id, $status, 'is_hidden');
        if ($res) {
            return Show::success();
        } else {
            return Show::error();
        }
    }


}